package com.college.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.college.domain.CourseDetails;
import com.college.facade.impl.CourseDetailsFacadeImpl;

@Controller
public class CourseDetailsController {
	
	@Autowired
	private CourseDetailsFacadeImpl courseDetailsFacade;
	
	@RequestMapping("addCourseDetails")
	public ModelAndView addCourseDetails(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		
		CourseDetails courseDetails = new CourseDetails();
		
		if(courseDetailsFacade.addCourse(request, courseDetails)){
			return new ModelAndView("home");
		  
		}
		
		return new ModelAndView("courseDetails");

	}
	@RequestMapping("updateCourseDetails")
	public ModelAndView updateCourseDetails(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		
		CourseDetails courseDetails = new CourseDetails();
		
		if(courseDetailsFacade.updateCourse(request, courseDetails)){
			return new ModelAndView("courseDetailsModification");
		  
		}
		
		return new ModelAndView("courseDetailsDisplayToUpdate");

	}
	@RequestMapping("courseDetailsDisplayToUpdate")
	public ModelAndView courseDetailsDisplayToUpdate(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		
		String courseCode=request.getParameter("courseCode");
		ModelAndView modelAndView;
		CourseDetails courseDetails = courseDetailsFacade.getCourseDetailsForCourseCode(courseCode);
		
		if(courseDetails!=null){
			modelAndView = new ModelAndView("courseDetailsDisplayToUpdate");
			modelAndView.addObject("course", courseDetails);
		    return modelAndView;
		}else{
			modelAndView = new ModelAndView("courseDetailsModification");
			return modelAndView;
		}

	}

}
